<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="域名" prop="domainName">
        <el-input
          v-model="queryParams.domainName"
          placeholder="请输入域名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="用户名称" prop="username">
        <el-input
          v-model="queryParams.username"
          placeholder="请输入用户名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="电话" prop="phone">
        <el-input
          v-model="queryParams.phone"
          placeholder="请输入用户电话"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="邮箱" prop="email">
        <el-input
          v-model="queryParams.email"
          placeholder="请输入用户邮箱"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="所属云" prop="cloudProviderId">
        <el-select v-model="queryParams.cloudProviderId" placeholder="请选择所属云" clearable>
          <el-option
            v-for="cloud in cloudProviderList"
            :key="cloud.id"
            :label="cloud.name"
            :value="cloud.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="映射服务器" prop="serverId">
        <el-select v-model="queryParams.serverId" placeholder="请选择映射服务器" clearable>
          <el-option
            v-for="server in serverList"
            :key="server.id"
            :label="server.name"
            :value="server.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="是否自动续期" prop="autoRenew">
        <el-select v-model="queryParams.autoRenew" placeholder="请选择是否自动续期" clearable>
          <el-option
            v-for="dict in dict.type.cert_yes_no"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="续后是否删除" prop="deleteOldCert">
        <el-select v-model="queryParams.deleteOldCert" placeholder="请选择续后是否删除" clearable>
          <el-option
            v-for="dict in dict.type.cert_yes_no"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable>
          <el-option
            v-for="dict in dict.type.cert_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['auto:certificate:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['auto:certificate:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['auto:certificate:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['auto:certificate:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="certificateList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="域名" align="center" prop="domainName" />
      <el-table-column label="证书ID" align="center" prop="certificateId" />
      <el-table-column label="用户名称" align="center" prop="username" />
      <el-table-column label="用户电话" align="center" prop="phone" />
      <el-table-column label="用户邮箱" align="center" prop="email" />
      <el-table-column label="所属云厂商" align="center" prop="cloudProviderId" >
        <template slot-scope="scope">
          <ListValueMapper :data-list="cloudProviderList" :value="scope.row.cloudProviderId"/>
        </template>
      </el-table-column>
      <el-table-column label="所属服务器" align="center" prop="serverId" >
        <template slot-scope="scope">
          <ListValueMapper :data-list="serverList" :value="scope.row.serverId"/>
        </template>
      </el-table-column>
      <el-table-column label="证书文件名" align="center" prop="certFileName" />
      <el-table-column label="证书本地存储路径" align="center" prop="certLocalPath" />
      <el-table-column label="证书在服务器的完整路径" align="center" prop="certServerPath" />
      <el-table-column label="是否自动续期" align="center" prop="autoRenew">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.cert_yes_no" :value="scope.row.autoRenew"/>
        </template>
      </el-table-column>
      <el-table-column label="续后是否删除" align="center" prop="deleteOldCert">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.cert_yes_no" :value="scope.row.deleteOldCert"/>
        </template>
      </el-table-column>
      <el-table-column label="到期前多少天续期" align="center" prop="renewBeforeDays" />
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.cert_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="证书颁发时间" align="center" prop="issueTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.issueTime, '{y}-{m}-{d} {h}:{m}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="证书过期时间" align="center" prop="expireTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.expireTime, '{y}-{m}-{d} {h}:{m}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="最后续期时间" align="center" prop="lastRenewTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.lastRenewTime, '{y}-{m}-{d} {h}:{m}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="最后同步到服务器时间" align="center" prop="lastSyncTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.lastSyncTime, '{y}-{m}-{d} {h}:{m}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="描述" align="center" prop="description" />
      <el-table-column label="更新时间" align="center" prop="updatedTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updatedTime, '{y}-{m}-{d} {h}:{m}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="100px">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['auto:certificate:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['auto:certificate:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改域名管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="60%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="域名" prop="domainName">
          <el-input v-model="form.domainName" placeholder="请输入域名" />
        </el-form-item>
        <el-form-item label="证书名称" prop="certificateName">
          <el-input v-model="form.certificateName" placeholder="请输入证书名称" />
        </el-form-item>
        <el-form-item label="证书ID" prop="certificateId">
          <el-input v-model="form.certificateId" placeholder="请输入证书ID" />
        </el-form-item>
        <el-form-item label="订单ID" prop="orderId">
          <el-input v-model="form.orderId" placeholder="请输入订单ID" />
        </el-form-item>
        <el-form-item label="用户名称" prop="username">
          <el-input v-model="form.username" placeholder="请输入用户名称" />
        </el-form-item>
        <el-form-item label="用户电话" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入用户电话" />
        </el-form-item>
        <el-form-item label="用户邮箱" prop="email">
          <el-input v-model="form.email" placeholder="请输入用户邮箱" />
        </el-form-item>
        <el-form-item label="所属云" prop="cloudProviderId">
          <el-select v-model="form.cloudProviderId" placeholder="请选择所属云" clearable>
            <el-option
              v-for="cloud in cloudProviderList"
              :key="cloud.id"
              :label="cloud.name"
              :value="cloud.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="映射服务器" prop="serverId">
          <el-select v-model="form.serverId" placeholder="请选择映射服务器" clearable>
            <el-option
              v-for="server in serverList"
              :key="server.id"
              :label="server.name"
              :value="server.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="证书文件名" prop="certFileName">
          <el-input v-model="form.certFileName" placeholder="请输入证书文件名" />
        </el-form-item>
        <el-form-item label="证书存储路径" prop="certLocalPath">
          <el-input v-model="form.certLocalPath" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="证书在服务器的完整路径" prop="certServerPath">
          <el-input v-model="form.certServerPath" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="是否自动续期" prop="autoRenew">
          <el-radio-group v-model="form.autoRenew">
            <el-radio
              v-for="dict in dict.type.cert_yes_no"
              :key="dict.value"
              :label="parseInt(dict.value)"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="续后是否删除" prop="deleteOldCert">
          <el-radio-group v-model="form.deleteOldCert">
            <el-radio
              v-for="dict in dict.type.cert_yes_no"
              :key="dict.value"
              :label="parseInt(dict.value)"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="前n天续期" prop="renewBeforeDays">
          <el-input v-model="form.renewBeforeDays" type="number" placeholder="请输入到期前多少天续期" />
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-select v-model="form.status" placeholder="请选择状态">
            <el-option
              v-for="dict in dict.type.cert_status"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="描述" prop="description">
          <el-input v-model="form.description" type="textarea" placeholder="请输入描述" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listCertificate, getCertificate, delCertificate, addCertificate, updateCertificate } from "@/api/auto/certificate";
import { listProvider } from '@/api/auto/provider';
import { listServer } from '@/api/auto/server';

export default {
  name: "Certificate",
  dicts: ['cert_status', 'cert_yes_no', 'cert_validate_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 域名管理表格数据
      certificateList: [],
      // 所属云数据
      cloudProviderList: [],
      // 映射服务器数据
      serverList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        domainName: null,
        certificateName: null,
        certificateId: null,
        orderId: null,
        productCode: null,
        username: null,
        phone: null,
        email: null,
        validateType: null,
        cloudProviderId: null,
        serverId: null,
        certFileName: null,
        certLocalPath: null,
        certServerPath: null,
        autoRenew: null,
        deleteOldCert: null,
        renewBeforeDays: null,
        status: null,
        issueTime: null,
        expireTime: null,
        lastRenewTime: null,
        lastSyncTime: null,
        description: null,
        createdTime: null,
        updatedTime: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        domainName: [
          { required: true, message: "域名不能为空", trigger: "blur" }
        ],
        cloudProviderId: [
          { required: true, message: "所属云不能为空", trigger: "blur" }
        ],
        serverId: [
          { required: true, message: "映射服务器不能为空", trigger: "blur" }
        ],
      }
    }
  },
  created() {
    this.getCloudList();
    this.getServerList();
    this.getList();
  },
  methods: {
    /** 查询域名管理列表 */
    getList() {
      this.loading = true
      listCertificate(this.queryParams).then(response => {
        this.certificateList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 获取所有云厂商信息
    getCloudList() {
      this.loading = true
      listProvider().then(response => {
        this.cloudProviderList = response.rows
        this.loading = false
      })
    },
    // 获取所有服务器信息
    getServerList() {
      this.loading = true
      listServer().then(response => {
        this.serverList = response.rows
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        domainName: null,
        certificateName: null,
        certificateId: null,
        orderId: null,
        productCode: null,
        username: null,
        phone: null,
        email: null,
        validateType: null,
        cloudProviderId: null,
        serverId: null,
        certFileName: null,
        keyFileName: null,
        certLocalPath: null,
        keyLocalPath: null,
        certServerPath: null,
        keyServerPath: null,
        autoRenew: null,
        deleteOldCert: null,
        renewBeforeDays: null,
        status: null,
        issueTime: null,
        expireTime: null,
        lastRenewTime: null,
        lastSyncTime: null,
        description: null,
        createdTime: null,
        updatedTime: null
      }
      this.resetForm("form")
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm")
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = "添加域名管理"
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      getCertificate(id).then(response => {
        this.form = response.data
        this.open = true
        this.title = "修改域名管理"
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateCertificate(this.form).then(response => {
              this.$modal.msgSuccess("修改成功")
              this.open = false
              this.getList()
            })
          } else {
            addCertificate(this.form).then(response => {
              this.$modal.msgSuccess("新增成功")
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal.confirm('是否确认删除域名管理编号为"' + ids + '"的数据项？').then(function() {
        return delCertificate(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess("删除成功")
      }).catch(() => {})
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('auto/certificate/export', {
        ...this.queryParams
      }, `certificate_${new Date().getTime()}.xlsx`)
    }
  }
}
</script>
